import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


public class HomeWork4 {

    public static Map<String,List<MovieSimValue>> map;

    public static Map<String,String> movie;


    public static void main(String[] args) throws IOException {
        readFile();
        readMovie();

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入查询的MovieID：");
        String id = scanner.next();

        if (map.containsKey(id)){
            List<MovieSimValue> list = map.get(id);
            Collections.sort(list , (MovieSimValue b1, MovieSimValue b2) -> b2.getSimValue().compareTo(b1.getSimValue()));
            System.out.println("Inputed Movie:");
            System.out.println(id+" - "+movie.get(id));
            System.out.println("Recommender movies:");
            for (int i = 0; i<15;i++){
                System.out.println(list.get(i).getMovieId()+" - "+movie.get(list.get(i).getMovieId()));
            }
        }else {
            System.out.println("无该影片");
        }


    }

    public static void readFile() throws IOException {
        map = new HashMap<>();

        File file;
        FileReader fr;
        BufferedReader br;
        file = new File("C://Users/13353/Desktop/sim_table.txt");
        if (!file.exists()) {
            System.out.println("\"" + "C://Users/13353/Desktop/sim_table.txt" + "\" does not exsit!");
            return;
        }
        fr = new FileReader(file);
        br = new BufferedReader(fr);
        String lineText;
        while (br.ready()) {
            List<MovieSimValue> list = new ArrayList<>();
            lineText = br.readLine();
            lineText = new String(lineText.getBytes(), "UTF-8");


            String[] info = lineText.split(":");

            String[] values = info[1].split("\t");
            for (String item: values) {

                String[] items = item.split(",");
                MovieSimValue movieSimValue = new MovieSimValue();
                movieSimValue.setMovieId(items[0]);
                movieSimValue.setSimValue(items[1]);
                list.add(movieSimValue);
            }

            map.put(info[0],list);
        }
        br.close();
    }

    public static void readMovie() throws IOException {
        movie = new HashMap<>();

        File file;
        FileReader fr;
        BufferedReader br;
        file = new File("C://Users/13353/Desktop/movies.dat");
        if (!file.exists()) {
            System.out.println("\"" + "C://Users/13353/Desktop/movies.dat" + "\" does not exsit!");
            return;
        }
        fr = new FileReader(file);
        br = new BufferedReader(fr);
        String lineText;
        while (br.ready()) {
            lineText = br.readLine();
            lineText = new String(lineText.getBytes(), "UTF-8");
            String[] info = lineText.split("::");
            movie.put(info[0],info[1]);
        }
        br.close();
    }
}
